import React, { Component } from 'react'
import { connect } from 'react-redux'
import { bindActionCreators } from 'redux'
import { Link } from 'react-router'
import { Table, Spin, Form, Row, Col, Input, Button, Select, TreeSelect, DatePicker, Checkbox, message, Dropdown, Menu, Icon, Popover } from 'antd'
import _ from 'lodash'
import moment from 'moment'

import Ajax from 'lib/Ajax'
import { addConfig } from 'actions/config'
import FilterModule from './components/filterRegistingCitizen'
import { getNewTreeData, findRowData, getNameByCode, confirmDelete, download, getNameByState } from 'lib/fun'

const FormItem = Form.Item;
const Option = Select.Option;

class ModuleName extends Component {
	constructor(props) {
    super(props);
    this.state = {
      loading: false,
      data: [],
    }
    this.current = 1;   //当前第几页
    this.total = 10;   //总共多少页
    this.pageSize = 10;   //每页多少行
    this.search = {};
  }

  componentDidMount(){
    const { config, addConfig } = this.props;
    if(!config.bankCode){
      // 所属银行
      addConfig('bankCode', 31);
    }
    if(!config.payCycleCode){
      // 发放周期
      addConfig('payCycleCode', 9);
    }
    // citizen_info_type_code  人员分类没有找着
    if(this.props.common.user){
      this.loadData(this.props.common.user.user.areaId)
    }
  }

  componentWillReceiveProps(nextProps){
    if(!this.props.common.user&&nextProps.common.user&&nextProps.common.user.user){
      this.loadData(nextProps.common.user.user.areaId)
    }
  }

  pageOnChange = (page)=>{
    this.current = page;
    this.loadData();
  }

  showSizeChange = (current, pageSize)=>{
    var page = Math.ceil(((this.current-1)*this.pageSize+1)/pageSize);
    this.pageSize = pageSize;
    this.current = page;
    this.loadData();
  }

  loadData = (id) => {
    var self = this;
    if(!this.props.common.user&&!id){
      return
    }
    self.setState({
      loading: true
    });
    if(!this.search.areaId){
      this.search.areaId = id?id:this.props.common.user.user.areaId
    }
    if(this.search.projectCategoryId){
      this.search.projectCategoryId = parseInt(this.search.projectCategoryId);
    }
    Ajax({
      url: '/projectRegister/listCitizen',
      data: Object.assign({
        currentPage: self.current,  //请求页码
        perPage: self.pageSize,   //每页数量
      }, this.search),
      success: function(data){
        self.total = data.totalCount;
        self.setState({
          loading: false,
          data: data.list
        });
      },
      error: function(error){
        self.setState({
          loading: false
        });
        message.error(error);
      }
    });
  }

  handleFilter = (option)=>{
    if(!_.isEqual(option, this.search)){
      this.search = option;
      this.loadData();
    }
  }
  
  render() {
    const { config, common } = this.props;
    const columns = [{
        title: '用户归属区划',
        dataIndex: 'areaName'
      }, {
        title: '姓名',
        dataIndex: 'name'
      }, {
        title: '身份证号码',
        dataIndex: 'identNumber'
      }, {
        title: '补贴年度',
        dataIndex: 'year'
      }, {
        title: '补贴项目',
        dataIndex: 'projectName'
      }, {
        title: '发放周期',
        dataIndex: 'payCycleCode',
        render: (text, record) => (<span>{ getNameByCode(config, 'payCycleCode', text) }</span>)
       }, {
        title: '发放次数',
        dataIndex: 'subsidyCount'
      }, {
        title: '发放银行',
        dataIndex: 'bankCode',
        render: (text, record) => (<span>{ getNameByCode(config, 'bankCode', text) }</span>)
      }, {
        title: '银行卡号',
        dataIndex: 'bankNumber'
      }, {
        title: '发放金额',
        dataIndex: 'subsidyMoney'
      }, {
        title: '发放状态',
        dataIndex: 'state',
        render: (text, record) => (<span>{ getNameByState(common, "projectRegisterEn", text) }</span>)
      }];

    var pagination = {
      current: this.current,
      total: this.total,
      pageSize: this.pageSize,
      onChange: this.pageOnChange,
      showSizeChanger: true,
      onShowSizeChange: this.showSizeChange,
      showTotal: total => `总共 ${total} 条`
    };

    return (
    	<div className="area">
        <FilterModule 
          handleFilter={ this.handleFilter }
        />
        <div className="area-con">
          <Table 
            columns={columns}
            dataSource={this.state.data}
            pagination={pagination}
            rowKey="registerId"
            loading={ this.state.loading }
          />
        </div>
    	</div>
    )
  }
}

function mapStateToProps(state) {
  return {
    config: state.config,
    common: state.common
  }
}

function mapDispatchToProps(dispatch) {
  return bindActionCreators({ addConfig }, dispatch)
}

module.exports = connect(mapStateToProps,mapDispatchToProps)(ModuleName)